//
//  YDStarView.m
//  YunDongGym
//
//  Created by lqq on 2018/5/22.
//  Copyright © 2018年 lqq. All rights reserved.
//

#import "YDStarView.h"
@interface YDStarView ()
@property (nonatomic, strong) NSMutableArray *stars;
@property (nonatomic, assign) CGFloat starWidth;//默认25
@end

@implementation YDStarView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self config];
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self =[super initWithCoder:aDecoder];
    if (self) {
        [self config];
    }
    return self;
}

- (void)config
{
    _starType = StarType_25;
    _starWidth = 25;
}


- (void)setStarCount:(NSInteger)starCount
{
    _starCount = starCount;
    [self removeAllSubviews];
    self.stars = [NSMutableArray array];
    for (int i = 0 ; i < starCount; i++) {
        UIImageView *star = [[UIImageView alloc] init];
//        star.contentMode = UIViewContentModeCenter;
        [self addSubview:star];
        [star mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(_starWidth));
            make.left.equalTo(self).offset(i*(_starWidth));
            make.centerY.equalTo(self);
        }];
        if (i < _score) {
            switch (_starType) {
                case StarType_25:
                {
                    star.image = IMG_NAME(@"ic_50_star_selected");
                }
                    break;
                case StarType_18:
                {
                    star.image = IMG_NAME(@"ic_36_star_selected");
                }
                    break;
                    
                default:
                    break;
            }
            
        } else {
            switch (_starType) {
                case StarType_25:
                {
                    star.image = IMG_NAME(@"ic_50_star_disable");
                }
                    break;
                case StarType_18:
                {
                    star.image = IMG_NAME(@"ic_36_star_normal");
                }
                    break;
                    
                default:
                    break;
            }
            
        }
        [self.stars addObject:star];
    }
}


- (void)setStarType:(StarType)starType
{
    _starType = starType;
    switch (starType) {
        case StarType_25:
            _starWidth = 25;
            break;
        case StarType_18:
            _starWidth = 18;
            break;
            
        default:
            break;
    }
    for (int i = 0 ; i < self.stars.count; i++) {
        UIImageView *star = self.stars[i];
        [star mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.height.equalTo(@(_starWidth));
            make.left.equalTo(self).offset(i*(_starWidth));
        }];
        
    }
}


- (void)setScore:(NSInteger)score
{
    _score = score;
    for (int i = 0 ; i < self.stars.count; i++) {
        UIImageView *star = self.stars[i];
        if (i < _score) {
            switch (_starType) {
                case StarType_25:
                {
                    star.image = IMG_NAME(@"ic_50_star_selected");
                }
                    break;
                case StarType_18:
                {
                    star.image = IMG_NAME(@"ic_36_star_selected");
                }
                    break;
                    
                default:
                    break;
            }
            
        } else {
            switch (_starType) {
                case StarType_25:
                {
                    star.image = IMG_NAME(@"ic_50_star_disable");
                }
                    break;
                case StarType_18:
                {
                    star.image = IMG_NAME(@"ic_36_star_normal");
                }
                    break;
                    
                default:
                    break;
            }
        }
    }
    
}


/**
 手指滑动时判选中第几个星星
 */
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
//    int x = point.x;
//    int y = point.y;
//
//    if (y>=(self.height / 2 - _starWidth / 2) && y<=(self.height / 2 + _starWidth / 2))     //Y在星星同一行
//    {
//        if (x >= 0 && x <= (_starCount*(_starWidth + _starSpace) - _starSpace))   //在星星范围内
//        {
//            self.selectCount = x / (_starWidth + _starSpace);
//        }
//    }
    
    for (int i = 0 ; i < self.stars.count; i++) {
        UIImageView *star = self.stars[i];
        if (CGRectContainsPoint(star.frame, point)) {
            self.score = i+1;
            break;
        }
    }
}

/**
 点击结束时判断选择的星星位置
 */
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    CGPoint point = [[touches anyObject] locationInView:self];
//    int x = point.x;
//    int y = point.y;
//    NSLog(@"%f~~~%f",self.centerY,_starWidth / 2);
//    if (y>=(self.height / 2 - _starWidth / 2) && y<=(self.height / 2 + _starWidth / 2))     //Y在星星同一行
//    {
//        if (x >= 0 && x <= (_starCount*(_starWidth + _starSpace) - _starSpace))   //在星星范围内
//        {
//            self.selectCount = x / (_starWidth + _starSpace);
//        }
//    }
    for (int i = 0 ; i < self.stars.count; i++) {
        UIImageView *star = self.stars[i];
        if (CGRectContainsPoint(star.frame, point)) {
            self.score = i+1;
            if (_choiceBlock) {
                _choiceBlock(i+1);
            }
            break;
        }
    }
}
@end
